	
	*Matt Duke computer 
*	cd "C:\Users\msj22\Dropbox\research\wrongful_discharge\intermediate_dtas"
	cd "C:\Users\msj22\Dropbox\research\wrongful_discharge\replication_May_2022\intermediate_dtas"
	global start 2005 
	*How many iterations? 
	global S 10000
	
	set seed 12345 
	
	
	use osha_nsc2, clear
	keep if year <= $start


	*First, save the actual estimate, to put in the eventual figure 
		global weight = "[aw = sh_emp1979]"
		global sample = "((state_run_office!=1 & year>=1979) | (state_run_office==1 & year>=1992)) & sector == 1" 
		global fes = "year_div state_code "
		global fes = "year state_code "

		reghdfe ln_acc_rateY_matt wdlapY $weight if sector == 1 & $sample, absorb($fes) vce(cluster state_code) 
		global actual = _b[wdlapY] 
		
	gen year_wdl = year if wdlapY > 0 & missing(wdlapY)!=1 
	egen first_yr_wdl = min(year_wdl), by(state)
	*missing means never passed PPE 
		replace first_yr_wdl = 2021 if first_yr_wdl==.

	preserve 
		keep state first_yr_wdl 
		duplicates drop 
	
		*Create a matrix with 50 rows, and order the "year of WDL passage" based on how many states passed in that year 
		matrix first_years = J(50, 1, .)
		
		sort first_yr_wdl 
		mkmat first_yr_wdl, matrix(first_yr_wdl) 
		
	restore 

	*Iteratively, assign placebo first PPE years based on empirical distribution, then save the coefficient from a regresion
		matrix estimates = J($S, 1, .)
	
	forvalues i = 1/$S {
	di `i' 
		qui {
				
			preserve 
				keep state 
				duplicates drop 
				gen random = runiform()
				sort random 
				svmat first_yr_wdl 
				rename first_yr_wdl fake_first_yr_wdl 
				
				drop random 
				tempfile fake 
				save `fake', replace 
			restore 
				
			merge m:1 state using `fake', keep(master match) nogen 
			gen fake_ppe = 0 
			replace fake_ppe = 1 if year>=fake_first_yr_wdl 
		
		
		*Run regression on fake treatment 
			reghdfe ln_acc_rateY_matt fake_ppe $weight if sector == 1 & $sample, absorb($fes) vce(cluster state_code) 
			matrix estimates[`i', 1] = _b[fake_ppe]
						
			drop fake_first_yr_wdl fake_ppe 
		} // qui 
	}
					
	
	preserve 
	clear 
	svmat estimates 
	sum estimates1, d
	save placebo_estimates_state_year.dta, replace 

	
	u placebo_estimates_state_year.dta, clear 
	sum estimates1, d
	count if abs(estimates1)>= abs(-0.137)
	
	hist estimates1, xline($actual) title("Distribution of estimates on placebo public policy exception") subtitle("Specification with state and year fixed effects") note("") xtitle("Placebo estimate")
*	graph export ../regression_results/placebo_treatment_effets_state_year.pdf, replace 
	
*	kdensity estimates1, xline($actual) title("Distribution of estimates on placebo treatment") note("")
	
	restore 
	